package cn.glutnn.oss.adapter;

import cn.glutnn.oss.entity.FileInfo;
import cn.glutnn.oss.utils.MinioUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/12/5 下午1:34
 * Description:
 */

public class MinioStorageAdapter implements StorageAdapter {
    @Resource
    private MinioUtil minioUtil;

    @Override
    @SneakyThrows
    public void createBucket(String bucketName) {
        minioUtil.createBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public String uploadFile(MultipartFile file, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        if (objectName != null) {
            return minioUtil.uploadFile(file, bucket, objectName + "/" + file.getOriginalFilename());
        } else {
            return minioUtil.uploadFile(file, bucket, file.getName());
        }

    }

    @Override
    @SneakyThrows
    public List<String> listBuckets() {
        return minioUtil.listBuckets();
    }

    @Override
    public List<FileInfo> getAllFiles(String bucketName) {
        return minioUtil.getAllFiles(bucketName);
    }

    @Override
    @SneakyThrows
    public InputStream downloadFile(String bucketName, String objectName) {
        return minioUtil.downloadFile(bucketName, objectName);
    }

    @Override
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioUtil.removeBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void removeFile(String bucketName, String objectName) {
        minioUtil.removeFile(bucketName, objectName);
    }

    @Override
    @SneakyThrows
    public void copyObject(String sourceBucket, String sourceObject, String targetBucket, String targetObject) {
        minioUtil.copyObject(sourceBucket, sourceObject, targetBucket, targetObject);
    }

    @Override
    @SneakyThrows
    public String getObjectInfo(String bucket, String objectName) {
        return minioUtil.getObjectInfo(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
        return minioUtil.getPresignedObjectUrl(bucketName, objectName, expires);
    }

    @Override
    @SneakyThrows
    public List<FileInfo> listAllFile() {
        return minioUtil.listAllFile();
    }
}
